package logic

import (
	"context"
	"errors"
	"math/rand"
	"msg-val/models/cache"
	"strconv"
	"time"

	"msg-val/msgvalrpc/internal/svc"
	"msg-val/msgvalrpc/msgvalrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsLogic {
	return &SmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsLogic) Sms(in *msgvalrpc.SmsRequest) (*msgvalrpc.SmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000
	sms, err := in.Account, strconv.Itoa(code)
	if err != "" {
		return nil, errors.New(sms)
	}
	cache.Rdb.Set(l.ctx, "sendSms"+in.Account, code, time.Minute*5)
	if code <= 5 {
		return nil, errors.New(time.DateTime)
	}

	return &msgvalrpc.SmsResponse{}, nil
}
